#!lua

sendmsg = function(cmd)
  local !,r in swi
  cmd = 'Message StrongED_ExecCmds '..cmd
  block(0,cmd)
  r[0] = block
  swi.! "OS_CLI"
end -- sendmsg

breakquote = function()
 local infile = arg[1]
 local cmd = ''
-- if not infile then
--    _ERRORMESSAGE("cannot open "..infile)
--    end
  quotes = {}
  for line in io.lines(infile) do
    match = line:gmatch("%[/?quote[^%]]*%]")
    repeat
      found = match(line)
      if found then
        if found == "[/quote]" then
          table.remove(quotes, table.maxn(quotes))
        else
          table.insert(quotes,found)
        end -- if
      end -- if
    until not found
  end -- for

  cmd = ""
  for n = 1,table.maxn(quotes) do
    cmd = cmd..' InsertStr("[/quote]") Newline'
  end -- for
  sendmsg(cmd)

  sendmsg('Newline Push Newline Newline')

  cmd = ""
  for i,v in pairs(quotes) do
    v = v:gsub('"', '\\"')
    cmd = cmd..' InsertStr("'..v..'") Newline'
  end -- for
  sendmsg(cmd..' Pop')

--  cmd = ""
--  for n = 1,table.maxn(quotes) do
--    cmd = cmd.." CaretUp"
--  end -- for
--  sendmsg(cmd)

end -- breakquote

block = block.new(256)
breakquote()
